/**
 * 
 */
package org.vsg.stock.basic.service.volfigure.high;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.basic.domain.VolFigureDetails;
import org.vsg.stock.basic.domain.VolFigureKlineDaily;
import org.vsg.stock.basic.domain.VolFigureKlineDailyPK;
import org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;

/**
 *  高量柱模式"high-1c-1a"
 * @author Bill Vison
 *
 */
public class High1c1aProcessor implements VolFigureKlineDailyProcessor {

	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	private Set<String> dailyKeys = new HashSet<String>();	

	/**
	 * set the logger for developer code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(High1c1aProcessor.class);
	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#processDailyData(java.util.List)
	 */
	@Override
	public List<VolFigureKlineDaily> processDailyData(
			List<StockDailyKLine> stkDailyKLineList) {
		
		if ( logger.isDebugEnabled() ) {
			logger.debug("High Process Stock ["+stkDailyKLineList.get(0).getId().getStockCode()+"]");
		}

		List<VolFigureKlineDaily> klineDailyList = new Vector<VolFigureKlineDaily>();
		
		LinkedList<Date> crestsDateList = new LinkedList<Date>();
		for (ZigIndicatorResultItem resultItem : this.crests) {
			StockDailyKLine dailyKLine = (StockDailyKLine)resultItem.getAssoRef();
			crestsDateList.add( dailyKLine.getDate() );
		}
		

		// --- 高量柱与高量柱之间至少有3天的间隔 ---
		StockDailyKLine kline = null;
		for (int i = 0 ; i < stkDailyKLineList.size() ; i++) {
			kline = stkDailyKLineList.get(i);
			
			if ( crestsDateList.remove( kline.getDate() ) ) {
				
				int lastThreeDays  = i+3;
				int checkDaysDef = lastThreeDays > crests.size() ? crests.size() : lastThreeDays;
				long tmpMaxVol = 0;
				for (int j = i+1 ; j <= checkDaysDef ; j++) {
					if (j >=  stkDailyKLineList.size()) {
						break;
					}
					StockDailyKLine skline = stkDailyKLineList.get(j);
					if (skline.getVol() > tmpMaxVol) {
						tmpMaxVol = skline.getVol();
					}
				}
				
				// --- compare diff max vol ----
				if (kline.getVol() >= tmpMaxVol ) {
					
					// --- check the value key had existed ---
					String indexLine = kline.getId().getStockCode()+","+detail.getCode()+","+df.format( kline.getDate() );
					
					// --- key format example "SH00001,XXXX,2010-01-10"
					if (this.dailyKeys.contains( indexLine) ) {
						continue;
					}
					// --- mark the date ---
					VolFigureKlineDailyPK idPk = new VolFigureKlineDailyPK();
					idPk.setCodeVolFigureDetails( this.detail.getCode() );
					idPk.setStockCode( kline.getId().getStockCode() );
					idPk.setStockStartDate( kline.getId().getDate() );
					
					// --- gen new data object ---
					VolFigureKlineDaily dailyInst = new VolFigureKlineDaily();
					dailyInst.setId( idPk );
					dailyInst.setStockDateEnd( kline.getId().getDate() );
					
					klineDailyList.add( dailyInst );					

				}
			}
			
		}
		return klineDailyList;
	}
	
	private List<ZigIndicatorResultItem> crests;

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCrestsInCycle(java.util.List)
	 */
	@Override
	public void setCrestsInCycle(List<ZigIndicatorResultItem> crests) {
		this.crests = crests;

	}

	@Override
	public void setExistedVolFigureKineDaily(
			List<VolFigureKlineDaily> volFigureKlineDailyList) {
		// --- get the daily ---
		VolFigureKlineDailyPK id = null;
		for (VolFigureKlineDaily daily : volFigureKlineDailyList) {
			id = daily.getId();
			String keyLine = id.getStockCode()+ "," + id.getCodeVolFigureDetails()+"," + df.format( id.getStockStartDate() );
			dailyKeys.add( keyLine );
		}
		
	}

	private VolFigureDetails detail;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCurrentVolFigureDetails(org.vsg.stock.basic.domain.VolFigureDetails)
	 */
	@Override
	public void setCurrentVolFigureDetails(VolFigureDetails detail) {
		this.detail = detail;

	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setTroughsInCycle(java.util.List)
	 */
	@Override
	public void setTroughsInCycle(List<ZigIndicatorResultItem> troughs) {
		// TODO Auto-generated method stub

	}

}
